Public Class AccessArmory

#Region "Properties"

    Public Enum RegionType
        US
        EU
        TW
        KR
        CH
    End Enum

    Private mRegion As RegionType = RegionType.US

    Public Property Region() As RegionType
        Get
            Return mRegion
        End Get
        Set(ByVal value As RegionType)
            mRegion = value
        End Set
    End Property
#End Region

#Region "Initialize"
    Public RegionString() As String = {"US", "EU", "TW", "KR", "CH"}

    Private UrlArmory() As String = { _
        "http://www.wowarmory.com/", _
        "http://eu.wowarmory.com/", _
        "http://tw.wowarmory.com/", _
        "http://kr.wowarmory.com/", _
        "http://ch.wowarmory.com/" _
        }

    Private UrlCharacters() As String = { _
        "character-sheet.xml", _
        "character-talents.xml", _
        "character-reputation.xml", _
        "character-achievements.xml", _
        "character-statistics.xml", _
        "guild-info.xml", _
        "item-info.xml" _
        }

    Public Enum UrlCharactersInfo
        UrlCharacterSheet
        UrlChTalents
        UrlChReputation
        UrlChAchievements
        UrlChStatistics

        UrlGuildInfo
        UrlItemInfo
    End Enum

    Public Enum AchievementsCategories
        None = 0
        Summary = 0
        General = 92
        Quests = 96
        Exploration = 97
        PlayerVsPlayer = 95
        DungeonsAndRaids = 168
        Professions = 169
        Reputation = 201
        WorldEvents = 155
        FeatsOfStrength = 81
    End Enum

    Private UrlGuildInfo As String = UrlCharacters(UrlCharactersInfo.UrlGuildInfo)
    Private UrlItemInfo As String = UrlCharacters(UrlCharactersInfo.UrlItemInfo)

    Public Function GetUrlCharacters(ByVal UrlCharaInfo As UrlCharactersInfo) As String
        Return UrlCharacters(UrlCharaInfo)
    End Function

#End Region

#Region "Access Armory"

    Public Function GetArmoryCharacter(ByVal Realm As String, ByVal Name As String, ByRef XMLdoc As Xml.XmlDocument, ByVal UrlCharaInfo As UrlCharactersInfo, Optional ByVal Category As AchievementsCategories = AchievementsCategories.None) As Xml.XmlElement
        Dim XMLtext As String
        Dim XMLnode As Xml.XmlElement = Nothing

        XMLtext = GetArmory(Realm, Name, GetBaseUrl(GetRegionString(mRegion), UrlCharaInfo), "CharSheet", , Category)

        Dim achvPrefix As String = ""
        If XMLtext <> "" Then
            'My.Application.DoEvents()
            Try
                XMLdoc.LoadXml(XMLtext)
                Select Case UrlCharaInfo
                    Case UrlCharactersInfo.UrlCharacterSheet
                        XMLnode = XMLdoc.SelectSingleNode("page/characterInfo")
                    Case UrlCharactersInfo.UrlChTalents
                        XMLnode = XMLdoc.SelectSingleNode("page/characterInfo")
                    Case UrlCharactersInfo.UrlChAchievements
                        achvPrefix = "cate" & CInt(Category) & "_"
                        If Category = AchievementsCategories.None Then
                            XMLnode = XMLdoc.SelectSingleNode("page/achievements")
                        Else
                            XMLnode = XMLdoc.SelectSingleNode("achievements/category")
                        End If
                    Case Else
                        XMLnode = XMLdoc.SelectSingleNode("page/characterInfo")
                End Select
            Catch ex As Exception
                Return Nothing
            End Try
        End If

        Try
            XMLdoc.Save(My.Computer.FileSystem.CombinePath(My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData, "last_armory_" & achvPrefix & GetUrlCharacters(UrlCharaInfo)))
        Catch ex As Exception
        End Try

        Return XMLnode
    End Function

    Public Function GetArmoryGuildInfo(ByVal Realm As String, ByVal Name As String, ByRef XMLdoc As Xml.XmlDocument) As Xml.XmlElement
        Dim XMLtext As String
        Dim XMLnode As Xml.XmlElement = Nothing

        XMLtext = GetArmory(Realm, Name, UrlArmory(mRegion) & UrlGuildInfo, "GuildInfo")

        If XMLtext <> "" Then
            'My.Application.DoEvents()
            Try
                XMLdoc.LoadXml(XMLtext)
                XMLnode = XMLdoc.SelectSingleNode("page/guildInfo/guild/members")
            Catch ex As Exception
                Return Nothing
            End Try
        End If

        Try
            XMLdoc.Save(My.Computer.FileSystem.CombinePath(My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData, "last_armory_" & UrlGuildInfo))
        Catch ex As Exception
        End Try

        Return XMLnode
    End Function

    Public Function GetArmoryItemInfo(ByVal ItemID As Integer, ByRef XMLdoc As Xml.XmlDocument) As Xml.XmlElement
        Dim XMLtext As String
        Dim XMLnode As Xml.XmlElement = Nothing

        XMLtext = GetArmory("", "", UrlArmory(mRegion) & UrlItemInfo, "ItemInfo", ItemID)

        If XMLtext <> "" Then
            'My.Application.DoEvents()
            Try
                XMLdoc.LoadXml(XMLtext)
                XMLnode = XMLdoc.SelectSingleNode("page/itemInfo/item")
            Catch ex As Exception
                Return Nothing
            End Try
        End If

        Try
            XMLdoc.Save(My.Computer.FileSystem.CombinePath(My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData, "last_armory_" & UrlItemInfo))
        Catch ex As Exception
        End Try

        Return XMLnode
    End Function

    Public Function GetBaseUrl(ByVal Region As String, ByVal InfoType As UrlCharactersInfo) As String
        Dim url As String = ""

        url = UrlArmory(GetRegionType(Region)) & GetUrlCharacters(InfoType)

        Return url
    End Function

    Public Function GetUrlChara(ByVal BaseUrl As String, ByVal Realm As String, ByVal Name As String) As String
        Dim url As String = ""

        url = BaseUrl & "?r=" & replaceForURL(Realm) & "&n=" & replaceForURL(Name)

        Return url
    End Function

    Public Function GetUrlCharaAchieve(ByVal BaseUrl As String, ByVal Realm As String, ByVal Name As String, ByVal Category As AchievementsCategories) As String
        Dim url As String = ""

        url = BaseUrl & "?r=" & replaceForURL(Realm) & "&n=" & replaceForURL(Name) & "&c=" & CInt(Category)

        Return url
    End Function

    Public Function GetUrlItem(ByVal BaseUrl As String, ByVal Item As Integer) As String
        Dim url As String = ""

        url = BaseUrl & "?i=" & Item

        Return url
    End Function

    Public Function GetArmory(ByVal Realm As String, ByVal Name As String, ByVal url As String, Optional ByVal comment As String = "any", Optional ByVal item As Integer = 0, Optional ByVal category As AchievementsCategories = AchievementsCategories.None) As String

        'My.Application.DoEvents()
        System.Threading.Thread.Sleep(100)

        Dim WC As System.Net.WebClient = New System.Net.WebClient()
        WC.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows XP)")

        'My.Application.DoEvents()
        Dim data As Byte() = Nothing
        Try
            If item <> 0 Then
                data = WC.DownloadData(GetUrlItem(url, item))
            Else
                If category = AchievementsCategories.None Then
                    data = WC.DownloadData(GetUrlChara(url, Realm, Name))
                Else
                    data = WC.DownloadData(GetUrlCharaAchieve(url, Realm, Name, category))
                End If
            End If
        Catch ex As Exception
            Return ""
        End Try

        Dim enc As System.Text.Encoding = System.Text.Encoding.UTF8

        'My.Application.DoEvents()
        Dim XMLtext As String = ""
        Try
            XMLtext = enc.GetString(data)
        Catch ex As Exception
            Return ""
        End Try

        'My.Application.DoEvents()
        System.Threading.Thread.Sleep(100)

        Return XMLtext
    End Function

    Public Function replaceForURL(ByVal str As String) As String
        str = Replace(str, " ", "+")
        str = Replace(str, "'", "%27")

        Return str
    End Function

    Public Function GetRegionType(ByVal Region As String) As RegionType
        Select Case UCase(Region)
            Case RegionString(RegionType.US)
                Return RegionType.US
            Case RegionString(RegionType.EU)
                Return RegionType.EU
            Case RegionString(RegionType.TW)
                Return RegionType.TW
            Case RegionString(RegionType.KR)
                Return RegionType.KR
            Case RegionString(RegionType.CH)
                Return RegionType.CH
            Case Else
                Return RegionType.US
        End Select
    End Function

    Public Function GetRegionString(ByVal Region As RegionType) As String
        Select Case Region
            Case RegionType.US
                Return RegionString(RegionType.US)
            Case RegionType.EU
                Return RegionString(RegionType.EU)
            Case RegionType.TW
                Return RegionString(RegionType.TW)
            Case RegionType.KR
                Return RegionString(RegionType.KR)
            Case RegionType.CH
                Return RegionString(RegionType.CH)
            Case Else
                Return RegionString(RegionType.US)
        End Select
    End Function

    Public Function IsArmoryError(ByVal XmlText As String) As String

        Dim Xml As New Xml.XmlDocument
        If XmlText = "" Then
            Return "Armory server down or No network coneection."
        End If

        Try
            Xml.LoadXml(XmlText)
        Catch ex As Exception
            Return "Xml message is broken, guess some Netowork trouble occured."
        End Try

        Try
            If Xml.SelectSingleNode("page/errorhtml").Attributes("type").Value = "maintenance" Then
                Return "Armory server is under maintenance."
            End If
        Catch ex As Exception
        End Try

        Return False
    End Function
#End Region

End Class

